<?php
use \GatewayWorker\Lib\Gateway;


class actionbattle extends cmdbase
{
	public function onCmd($msg){

		//uid
		$client_id = $this->getCurClientId();
		$uid = Gateway::getUidByClientId($client_id);

		$msg_body_group = array(
			'type' => $msg['type'], 
			'fromuid' => $uid,
			'touid' => $msg['touid'],
			'content' => $msg['content']
		);
		$ret = $this->sendToGroup("notify_action", $msg_body_group);

		$msg_body = array();
		if($ret){
			$msg_body['ret'] = NetErrorCode::$None;
			$msg_body['reason'] = "success";

			//======记录状态 Start =======
			if($msg['type'] < ActionType::$Challenge){//只能记录骰子状态
				$group_mgr = $GLOBALS['GroupMgr'];
				$group_id = $this->getCurGroupId();
				$person_wrap = &$group_mgr->getBattlePersonWrap($group_id);
				if(isset($person_wrap)){
					$person_list = $person_wrap['persons'];
					//遍历对应的玩家，修改状态
					foreach ($person_list as $index => $person) {
						if($person['uid'] == $msg['touid']){
							$person['state'] = $msg['type'];
							if($msg['type'] == ActionType::$Shake)
								$person['content'] = $msg['content'];
							//echo "修改uid:", $msg['touid'], " state=", $person['state'], " content=", $person['content'], "\n";

							//修改这3行代码，才能修改成功。临时写法，后面重构
							$person_list[$index] = $person;
							$person_wrap['persons'] = $person_list;

							break;
						}
					}
				}
			}
			//======记录状态 End =======
		}else{
			$msg_body['ret'] = NetErrorCode::$Error;
			$msg_body['reason'] = "Fatal Error:group is miss";
		}


 		return $msg_body;
	}
}